package co.yixiang.modules.shop.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.shop.domain.Bank;
import co.yixiang.modules.shop.service.BankService;
import co.yixiang.modules.shop.service.dto.BankQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;

/**
* @author lijunze
* @date 2020-05-08
*/
@Api(tags = "贷款机构管理")
@RestController
@RequestMapping("/api/bank")
public class BankController {

    private final BankService bankService;

    public BankController(BankService bankService) {
        this.bankService = bankService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('bank:list')")
    public void download(HttpServletResponse response, BankQueryCriteria criteria) throws IOException {
        bankService.download(bankService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询贷款机构")
    @ApiOperation("查询贷款机构")
    @PreAuthorize("@el.check('bank:list')")
    public ResponseEntity<Object> getBanks(BankQueryCriteria criteria, Pageable pageable){
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",200);
        resultMap.put("message",bankService.queryAll(criteria,pageable));
        return ResponseEntity.ok(resultMap);
    }

    @PostMapping
    @Log("新增贷款机构")
    @ApiOperation("新增贷款机构")
    @PreAuthorize("@el.check('bank:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Bank resources){
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",200);
        resultMap.put("message",bankService.create(resources));
        return ResponseEntity.ok(resultMap);
    }

    @PutMapping
    @Log("修改贷款机构")
    @ApiOperation("修改贷款机构")
    @PreAuthorize("@el.check('bank:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Bank resources){
        bankService.update(resources);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",200);
        return ResponseEntity.ok(resultMap);


    }

    @Log("删除贷款机构")
    @ApiOperation("删除贷款机构")
    @PreAuthorize("@el.check('bank:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        bankService.deleteAll(ids);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",200);
        return ResponseEntity.ok(resultMap);

    }
}